package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Collect;
import com.example.demo.entity.Poetry;
import com.example.demo.mapper.CollectMapper;
import com.example.demo.mapper.PoetryMapper;
import com.example.demo.service.CollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private PoetryMapper poetryMapper;

    @Override
    public Integer insertCollect(Long userId, Integer poetryId) {
        return collectMapper.insert(new Collect(userId, poetryId));
    }

    @Override
    public Integer deleteCollect(Long userId, Integer poetryId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<Collect>();
        queryWrapper.eq("user_id",userId).eq("poetry_id",poetryId);
        return collectMapper.delete(queryWrapper);
    }

    @Override
    public List<Poetry> selectMyCollectPoetry(Long userId,Integer pageNo) {
        pageNo = (pageNo-1)*10;
        List<Integer> poetryIdList = collectMapper.getMyCollect(userId,pageNo);
        List<Poetry> result = new ArrayList<>();
        if (poetryIdList.size()==0){
            return result;
        }
        QueryWrapper<Poetry> queryWrapper = new QueryWrapper<Poetry>();
        queryWrapper.in("id",poetryIdList);
        result = poetryMapper.selectList(queryWrapper);
        return result;
    }
}
